#ifndef PLAINEDIT_H
#define PLAINEDIT_H

#include <QtGui>
#include "highlighter.h"

class LineNumberArea;

class PlainEdit : public QPlainTextEdit
{
    Q_OBJECT

public:
    PlainEdit(QWidget *parent = 0);
    ~PlainEdit();


    void lineNumberAreaPaintEvent(QPaintEvent *event);
    int lineNumberAreaWidth();

    void setCompleter(QCompleter *c);
    QCompleter *completer() const;

    QString filepath;
    bool saved;
    Highlighter *highlighter;

protected:
    void keyPressEvent(QKeyEvent *e);
    void focusInEvent(QFocusEvent *e);
    void resizeEvent(QResizeEvent *event);

private slots:
    void insertCompletion(const QString &completion);
    void updateLineNumberAreaWidth(int newBlockCount);
    void highlightCurrentLine();
    void updateLineNumberArea(const QRect &, int);
    void unsave();

private:
    QString textUnderCursor() const;

    QWidget *lineNumberArea;
    QCompleter *c;
};



class LineNumberArea : public QWidget
{
public:
    LineNumberArea(PlainEdit *editor) : QWidget(editor)
    {
        codeEditor = editor;
    }

    QSize sizeHint() const
    {
        return QSize(codeEditor->lineNumberAreaWidth(), 0);
    }

protected:
    void paintEvent(QPaintEvent *event)
    {
        codeEditor->lineNumberAreaPaintEvent(event);
    }

private:
    PlainEdit *codeEditor;
};
#endif // PLAINEDIT_H
